<?
session_start();
include('conn.php');
$opt = $_REQUEST["q"];
	switch($opt)
	{
		case '0':
			$qry1 = "SELECT * from replaceCard where status = '1' order by id desc;";
			$qry = mysql_query($qry1);
			$data = array();
			while($rows = mysql_fetch_array($qry))
			{
				$q1 = "select cardno from cardTable where id = '".$rows['oldcard']."'";
				$r1 = mysql_fetch_array(mysql_query($q1));
				$q2 = "select cardno from cardTable where id = '".$rows['newcard']."'";
				$r2 = mysql_fetch_array(mysql_query($q2));
				$q3 = "select stbno from stbTable where id = '".$rows['stbno']."'";
				$r3 = mysql_fetch_array(mysql_query($q3));				
				$data[] = array
					(			
						"id" => $rows['id'],			
						"oldcard" => $r1['cardno'],
						"newcard" => $r2['cardno'],
						"stb" => $r3['stbno'],
						"rdate" => $rows['replacedate'],
						"reason" => $rows['reason'],
						"comments" => $rows['comments']			
					);
			}
			echo json_encode($data);
			break;
			
		case '1':
			$oldcardno = $_REQUEST['oldcno'];
			$newcardno = $_REQUEST['newcno'];
			$qry1 = "SELECT stbno from stbTable where id in (select stbid from STBDetails where cardid in (select id from cardTable where cardno='$oldcardno')); ";
			$qry = mysql_query($qry1);			
			while($rows = mysql_fetch_array($qry))			
				$data = $rows['stbno'];								
			echo $data;								
			break;
			
		case '2':
			$qry1 = "SELECT cardno from cardTable where flag='occ' and status = '0' order by id ";
			$qry = mysql_query($qry1);			
			while($rows = mysql_fetch_array($qry))
			{
				$data[] = $rows['cardno']	;					
			}
			echo json_encode($data);
			break;
			
		case '3':
			$qry1 = "SELECT cardno from cardTable where flag='free' and status = '0' order by id ";
			$qry = mysql_query($qry1);			
			while($rows = mysql_fetch_array($qry))
			{
				$data[] = $rows['cardno']	;					
			}
			echo json_encode($data);
			break;
	}
		
?>